<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Coze 代理 Web</title>
    <style>
        :root {
            --neon-blue: #0ff0fc;
            --neon-purple: #9600ff;
            --dark-bg: #121212;
            --card-bg: #1e1e1e;
        }

        body {
            font-family: 'Segoe UI', sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            background: var(--dark-bg);
            color: #fff;
        }

        h2 {
            text-align: center;
            color: var(--neon-blue);
            margin-bottom: 30px;
            text-shadow: 0 0 10px var(--neon-blue);
        }

        form {
            display: flex;
            flex-direction: column;
            gap: 20px;
            padding: 25px;
            background: var(--card-bg);
            border-radius: 10px;
            box-shadow: 0 0 15px rgba(0, 240, 252, 0.2);
            border: 1px solid rgba(0, 240, 252, 0.3);
        }

        label {
            display: flex;
            flex-direction: column;
            gap: 8px;
            color: #ddd;
        }

        input,
        textarea {
            font-size: 1em;
            padding: 12px;
            border-radius: 6px;
            background: #2d2d2d;
            border: 1px solid #444;
            color: #fff;
            transition: all 0.3s;
        }

        input:focus,
        textarea:focus {
            outline: none;
            border-color: var(--neon-blue);
            box-shadow: 0 0 8px rgba(0, 240, 252, 0.4);
        }

        button {
            background: linear-gradient(45deg, var(--neon-purple), var(--neon-blue));
            color: #fff;
            border: none;
            padding: 12px;
            border-radius: 6px;
            cursor: pointer;
            font-weight: bold;
            letter-spacing: 1px;
            transition: all 0.3s;
            margin-top: 10px;
        }

        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 240, 252, 0.4);
        }

        button:disabled {
            background: #555;
            transform: none;
            box-shadow: none;
        }

        #result {
            margin-top: 25px;
            padding: 20px;
            background: var(--card-bg);
            border-radius: 10px;
            min-height: 60px;
            border: 1px solid rgba(0, 240, 252, 0.2);
            color: #ddd;
            line-height: 1.6;
            position: relative;
            overflow: hidden;
        }

        #result.loading {
            border: 1px solid var(--neon-blue);
            box-shadow: 0 0 15px rgba(0, 240, 252, 0.4);
            animation: pulseBorder 1.5s infinite alternate;
        }

        #result.loading span {
            display: inline-block;
            animation: slide 2s ease-in-out infinite;
        }

        @keyframes slide {

            0%,
            100% {
                transform: translateX(0);
            }

            50% {
                transform: translateX(100px);
            }
        }

        @keyframes pulseBorder {
            0% {
                border-color: var(--neon-blue);
                box-shadow: 0 0 5px rgba(0, 240, 252, 0.4);
            }

            100% {
                border-color: rgba(0, 240, 252, 0.7);
                box-shadow: 0 0 20px rgba(0, 240, 252, 0.8);
            }
        }

        .elapsed {
            margin-top: 15px;
            color: var(--neon-blue);
            font-size: 0.9em;
            text-align: right;
        }
    </style>
</head>

<body>
    <h2>Coze 代理 Web</h2>
    <form id="cozeForm">
        <label>要求：<textarea name="text" rows="3" placeholder="请输入内容" id="mainTextarea">高情商回复</textarea></label>
        <label>
            <div>图片：
                <div id="imagePreview" style="float:right;display:none;width:30px;margin-left:5px;">
                    <img id="previewImage" style="max-width:100%;height:100%;border-radius:6px;object-fit:cover;">
                </div>
                <input type="file" name="image" accept="image/*" id="imageInput" style="display:none;">
            </div>
            <button type="button"onclick="document.getElementById('imageInput').click()">选择图片</button>
        </label>
        <button type="submit">提交</button>
    </form>
    <div id="result"></div>
    <div class="elapsed">
        <span id="copiedTip" style="display: none;float: left;">已复制到剪贴板</span>
        <span id="elapsed" style="margin-top:8px;color:#888;font-size:0.95em;">耗时:0ms</span>
    </div>
    <script>
        const form = document.getElementById('cozeForm');
        const result = document.getElementById('result');
        const elapsed = document.getElementById('elapsed');
        const imageInput = document.getElementById('imageInput');
        const imagePreview = document.getElementById('imagePreview');
        const previewImage = document.getElementById('previewImage');

        // 使textarea获取焦点时全选
        const mainTextarea = document.getElementById('mainTextarea');
        if(mainTextarea){
            mainTextarea.addEventListener('focus', function(){
                this.select();
            });
        }

        imageInput.addEventListener('change', function (e) {
            if (this.files && this.files[0]) {
                const reader = new FileReader();
                reader.onload = function (e) {
                    previewImage.src = e.target.result;
                    imagePreview.style.display = 'block';
                }
                reader.readAsDataURL(this.files[0]);
            }
        });

        form.onsubmit = async (e) => {
            e.preventDefault();
            result.innerHTML = '<span>处理中...</span>';
            result.classList.add('loading');
            elapsed.textContent = '';
            const start = Date.now();
            const fd = new FormData(form);
            try {
                const res = await fetch('/coze', { method: 'POST', body: fd });
                const duration = Date.now() - start;
                result.classList.remove('loading');
                if (!res.ok) {
                    const err = await res.json();
                    result.textContent = '错误: ' + (err.error || res.status);
                    elapsed.textContent = '耗时: ' + duration + ' ms';
                } else {
                    const txt = await res.text();
                    result.textContent = txt;
                    elapsed.textContent = '耗时: ' + duration + ' ms';
                    let clipboardSupported = !!(navigator.clipboard && navigator.clipboard.writeText);
                    if (clipboardSupported) {
                        try {
                            // 先选中 result 区域文本
                            if (window.getSelection && document.createRange) {
                                var selection = window.getSelection();
                                var range = document.createRange();
                                range.selectNodeContents(result);
                                selection.removeAllRanges();
                                selection.addRange(range);
                            }
                            await navigator.clipboard.writeText(txt);
                            copiedTip.textContent = '已复制到剪贴板';
                            copiedTip.style.display = 'inline';
                            setTimeout(()=>{
                                copiedTip.style.display = 'none';
                                if (selection) selection.removeAllRanges();
                            }, 2000);
                        } catch(e) {
                            copiedTip.textContent = '自动复制失败，请手动长按复制';
                            copiedTip.style.display = 'inline';
                            setTimeout(()=>{
                                copiedTip.style.display = 'none';
                            }, 3000);
                        }
                    } else {
                        copiedTip.textContent = '请手动长按结果进行复制';
                        copiedTip.style.display = 'inline';
                        setTimeout(()=>{
                            copiedTip.style.display = 'none';
                        }, 3000);
                    }
                }
            } catch (err) {
                result.classList.remove('loading');
                result.textContent = '请求失败: ' + err;
                elapsed.textContent = '';
            }
        };</script>
</body>

</html>